package com.qimo.controller;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qimo.model.User;
import com.qimo.service.ShoppingCartService;
import com.qimo.service.UserService;
import com.qimo.tool.ResultInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class UserController {
    @Resource
    private ShoppingCartService shoppingCartService;
    @Resource
    private UserService userService;
    @ResponseBody
    @RequestMapping("/user/checklogin")
    public ResultInfo checklogin(HttpServletRequest request, HttpServletResponse response,@RequestBody User user) throws IOException {
        System.out.println("验证登陆");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        System.out.println(user.getUserName());
        User userTemp = this.userService.selectUser(user.getUserName());

        if(userTemp.getPassWord().equals(user.getPassWord()))//密码正确
        {
            ResultInfo resultInfo=new ResultInfo();
            resultInfo.setCode(1);
            userTemp.setNumber(shoppingCartService.selectShoppingCart(userTemp.getUserId()));//查购物车数量
            resultInfo.setMsg("验证登陆状态成功");
            resultInfo.setResult(userTemp);
            return resultInfo;
        }
        else {
            return ResultInfo.failure("密码错误,取消登录状态");
        }

    }


    @ResponseBody
    @RequestMapping("/user/register")
    public ResultInfo register(HttpServletRequest request, HttpServletResponse response, @RequestBody User user) throws IOException {
        System.out.println("进入register");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        if(this.userService.selectUser(user.getUserName())!=null)
            return  ResultInfo.failure("注册失败,账号名已存在");
        if(this.userService.insertUser(user)==1)
                return ResultInfo.success();
        else
            return ResultInfo.failure("主键冲突检查数据库，正常使用应该不可能");
        }

    @ResponseBody
    @RequestMapping("/user/Update")
    public ResultInfo Update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入register");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int userId = Integer.parseInt(request.getParameter("userId"));
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");
        String userShowName= request.getParameter("userShowName");
        String userRealName= request.getParameter("userRealName");
        int Sex = Integer.parseInt(request.getParameter("Sex"));
        String Address = request.getParameter("Address");
        String Email = request.getParameter("Email");
        String Telephone = request.getParameter("Telephone");
        String Mobile = request.getParameter("Mobile");
        String newPassWord = request.getParameter("newPassWord");
        User temp=this.userService.selectUser(userName);
        if(temp.getUserId()==userId&&temp.getPassWord().equals(passWord))//身份验证
        {
       if(this.userService.update(userId,userShowName,userRealName,Sex,Address,Email,Telephone,Mobile,newPassWord)>0)
           return ResultInfo.success();
       else
           return ResultInfo.failure("更新失败");
        }
        else return ResultInfo.failure("用户信息错误");

    }

    }


